# /**
# * Copyright (c) 2022 Beijing Jiaotong University
# * PhotLab is licensed under [Open Source License].
# * You can use this software according to the terms and conditions of the [Open Source License].
# *
# * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
# *
# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# *
# * See the [Open Source License] for more details.
# */
# /**
# * Author: Shihao Xie
# * Created: Feb. 16, 2023
# * Supported by: National Key Research and Development Program of China
# */
import os
import sys

sys.path.append("/Users/tyz/photlab/pyphot-server")
for i in sys.path:
    print(i)
from phot_server.server_config.global_config import COMPONENT_POSTFIX, LIBRARY_ROOT, BASE_ROOT
from phot_server.utils.phtc import parse_component_toml, save_phtc

# from server_config.global_config import COMPONENT_POSTFIX, LIBRARY_ROOT, BASE_ROOT
# from utils.phtc import parse_component_toml

print("@@@",COMPONENT_POSTFIX, LIBRARY_ROOT, BASE_ROOT)


component_list = []
icon_list = os.listdir(os.path.join(BASE_ROOT, "./phot_server/database/icon/"))


def raw2PhotComponent(path: str, target_path: str = None,is_debug=False):
    # path = path.replace("\\", os.sep).replace("/", os.sep)
    if os.path.exists(path) is False:
        os.mkdir(path)
    if os.path.isdir(path):
        for file in os.listdir(path):
            new_path = path + "/" + file
            new_target_path = target_path + "/" + file.replace(".toml", COMPONENT_POSTFIX)
            raw2PhotComponent(new_path, new_target_path)
    else:
        [parent_directory, full_name] = path.rsplit("/", 1)
        [name, suffix] = full_name.rsplit(".", 1)
        if suffix == "toml":
            component = parse_component_toml(path)
            print(target_path)
            component.path = target_path
            if component.iconName is None and component.name + ".svg" in icon_list:
                component.iconName = component.name + ".svg"
            else:
                component.iconName = "unknown.svg"
            # component.category = "."+parent_directory[1:]
            # component.path = component.category+component.name
            component_list.append(component)
            print(component)
            # if save:
            #     save_phtc(target_path, component)
    if is_debug:
        return component_list

from thrift.transport import TSocket, TTransport
from thrift.protocol import TJSONProtocol
from phot_server.rpc.library_service.Library import Client
from thrift.protocol import TBinaryProtocol


def save_to_search_engine():
    transport = TSocket.TSocket("127.0.0.1", 8080)
    transport_protocol = TTransport.TBufferedTransport(transport)
    protocol_factory = TBinaryProtocol.TBinaryProtocol(transport_protocol)
    client = Client(protocol_factory)
    transport.open()
    for component in component_list:
        print(component)
        client.save_component_prototype(component.path, component)
    transport.close()


if __name__ == '__main__':
    print(LIBRARY_ROOT)
    raw2PhotComponent(LIBRARY_ROOT + "/../raw_component_definition", LIBRARY_ROOT)
    # raw2PhotComponent("/")
    save_to_search_engine()

